package  com.syqc.sensor.fwdrev

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.BaseToolbarRecycleActivity
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.custom.LeftRightTextView
import com.syqc.entity.FwdRevInfo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.HeadFwdRevBinding
import com.syqc.utils.DataUtil
import com.syqc.utils.TimeUtil

class FwdRevInfoActivity : BaseToolbarRecycleActivity() {


    override fun initRecyclerView() {
        val adapter = FwdRevAdapter()
        mRecycler.adapter = adapter
        adapter.addHeaderView(initHeadView())
        DataUtil.fwdrevs?.let {
            ToolbarHelper.setToolbar(this, it.carPlate)
            adapter.setList(it.detail)
        }

    }

    override fun initHeadView(): View {
        val revBinding = HeadFwdRevBinding.inflate(layoutInflater)
        DataUtil.fwdrevs?.let {
            revBinding.lrvFwd.setRightText(TimeUtil.instance.formatTimeDesc(it.posiTime ?: 0))
            revBinding.lrvRev.setRightText(TimeUtil.instance.formatTimeDesc(it.negaTime ?: 0))
            revBinding.lrvStop.setRightText(TimeUtil.instance.formatTimeDesc(it.stopTime ?: 0))
            revBinding.lrvTimes.setRightText(it.count)
        }
        return revBinding.root
    }

    override fun setListener() {
        binding.tvToTop.setOnClickListener {
            mRecycler.smoothScrollToPosition(0)
        }

        mRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val position = mLayoutManger.findFirstVisibleItemPosition()
                binding.tvToTop.isVisible = position > 0
            }
        })
    }
}

class FwdRevAdapter : BaseQuickAdapter<FwdRevInfo, BaseViewHolder>(R.layout.item_fwd_rev) {
    override fun convert(holder: BaseViewHolder, item: FwdRevInfo) {
        val position = holder.layoutPosition
        val num = if (position > 9) "$position" else "0$position"
        holder.getView<TextView>(R.id.tv_item_num).text = num

        holder.getView<LeftRightTextView>(R.id.lrvFwdStart).setRightText(item.stime)
        holder.getView<LeftRightTextView>(R.id.lrvFwdEnd).setRightText(item.etime)
        holder.getView<LeftRightTextView>(R.id.lrvFwdState)
            .setRightText(getState(context, item.state))
        holder.getView<LeftRightTextView>(R.id.lrvFwdLong)
            .setRightText(TimeUtil.instance.formatTimeDesc(item.duration ?: 0))
        holder.setText(R.id.tv_start_address, item.saddr ?: "")
            .setText(R.id.tv_end_address, item.eaddr ?: "")
    }

    private fun getState(context: Context, state: String): String {
        return when (state) {
            "00" -> context.getString(R.string.desc_fwd)
            "01" -> context.getString(R.string.desc_rev)
            "02" -> context.getString(R.string.desc_stop)
            else -> state
        }
    }
}